<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252"></head><body>&lt;%@ EnableSessionState=true %&gt;
&lt;% Option Explicit
	dim thisurl
	thisURL=Request.ServerVariables("PATH_INFO") &amp; "?" &amp; Request.ServerVariables("QUERY_STRING")
	const title="Access your Outlook application using PHP"
    const ArticleID=6490
%&gt;
<!--#include virtual="/include/Banners/2.inc"-->
<!--#include virtual="/include/common_headerArticle.inc"-->
<table align="center" border="0" cellpadding="0" cellspacing="0" width="780">
<tbody><tr>

<td valign="top" width="125">
<!--#include virtual="/include/left_col.inc"-->
</td>

<td valign="top">



<!-- Content start -->



<!-- Content start -->


<table border="0" cellpadding="0" cellspacing="10" width="100%">
<tbody><tr align="left" valign="top">
<td>


<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr>
<td valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="1">
<b>Current area: <a href="http://www.programmersheaven.com/">HOME</a> -&gt; Access your Outlook application using PHP</b><br>
</font></td>
</tr>
</tbody></table>


<p align="center"><b><font face="Verdana" size="2">Access your Outlook application using PHP</font></b></p>
<p><font color="#000000" face="Verdana" size="2">I have always been surprised 
  from what PHP can do.Using COM objects shows new and best ways for PHP development. 
  The folowing code, which is designed like <i>Step by Step wizard</i>, shows 
  the access to Inbox and Outbox folders in your MS Outlook application.</font></p>
<p><font color="#000000" face="Verdana" size="2">You should do the folowing:</font></p>
<p><font color="#000000" face="Verdana" size="2"><b><font color="#FF0000">Step1:</font></b> 
  check the system configuration and registry entires.</font></p>
<p><font color="#000000" face="Verdana" size="2"><b>System</b>:<br>
  OS: Windows 98/NT/2000/XP <br>
  MS Outlook<br>PHP &gt;4.0.5</font></p>
<p><font color="#000000" face="Verdana" size="2"><b>Registry:</b></font></p>
<p><font color="#000000" face="Verdana" size="2">Start regedit.exe from Start 
  Menu-&gt;Run ,check the HKEY_CLASSES_ROOT section for the folowing entires</font></p>
<p><font color="#000000" face="Verdana" size="2"><img src="COM_Outlook_files/regedit.gif" height="185" width="402"> 
  <br>
  Outlook.Application and MIME.Session (or MAPI.Session1).If the MAPI.Session 
  is missing you shold do the folowing:</font></p>
<p><img src="COM_Outlook_files/outlook_app.gif" height="70" width="276"> <img src="COM_Outlook_files/mapi_session.gif" height="83" width="271"></p>
<p><font color="#000000" face="Verdana" size="2">1.Search your computer for the 
  file named cdo.dll if doesn't exists (cdo.dll is a part from many products like 
  xi-tec <a href="http://phoneoffice.xi-tec.com/">PhoneOffice</a>) you must download 
  it from the <b>Microsoft</b> web site .<br>
  2.Move your cdo.dll in the system32 directory and register it with the regsvr32.exe(just 
  drag the file and drop it over regsvr32.exe file).</font></p>
<p><font color="#000000" face="Verdana" size="2">After this first step you are 
  ready for the <b><font color="#FF0000">STEP 2</font></b> :Classes</font></p>
<p><font color="#000000" face="Verdana" size="2">Open your favorite PHP editor 
  open a new file and name it: <b>COutLook.php</b></font></p>
<p><font color="#000000" face="Verdana" size="2">It is time to write a simple 
  class:</font></p>
<pre><code><p><font size="2">&lt;?PHP</font></p>
<p><b><font color="#004080">global</font></b> <font color="#008080">$UnreadMessagesInFolder;</font>
  <font color="#004080"><b>class</b></font> <font color="#008080">COutLook</font>{
  function for retreiving messages from the selected folder (Inbox or Outbox)
  <font color="#004080"><b>function</b></font><font color="#008080"> getMessages</font>(<font color="#FF8080">$folder</font>){
</p>
<p>Setup the folder table,.there is 4 elements:</p>
<p>message number,message subject ,message type and date received
</p>
<p><b><font color="#004080">echo</font></b>"<font color="#008080">&lt;body 
  text=darkblue&gt;
  &lt;br&gt;&lt;font color=red face=verdana size=3&gt;&lt;b&gt;$folder&lt;/b&gt;&lt;/font&gt;
  &lt;table width=100%&gt;
  &lt;TR bgcolor=#EEEFFF&gt;&lt;td&gt;&lt;font face=verdana size=2&gt;N:&lt;/td&gt;&lt;td&gt;
  &lt;font face=verdana size=2&gt; Subject&lt;/td&gt;&lt;TD&gt;
  &lt;font face=verdana size=2 &gt;Type&lt;/TD&gt;&lt;TD&gt;&lt;font face=verdana 
  size=2&gt; Date&lt;/TD&gt;&lt;/TR&gt;</font>";</p>
<p>creating the <b>COM</b> instance for Outlook.application and MAPI session(access 
  the outlook folders object)
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);
  <font color="#FF8080">$session</font>= new <b><font color="#004080">COM</font></b>(<font color="#008080">"MAPI.Session"</font>);</p>
<p>
  Log into the session like default user 
  <font color="#FF8080">$session</font><font color="#804040">-&gt;Logon();</font></p>
<p>selecting working folder Inbox ot Outbox/
  <font color="#FF8080">$inb=$session</font><font color="#804040">-&gt;$folder;</font></p>
<p>get the total messages in Folder
  <font color="#FF8080">$messages=$inb</font><font color="#804040">-&gt;Messages-&gt;Count()</font>;</p>
<p>get the elements of the message object</p>
<p><b><font color="#004080">for</font></b>(<font color="#FF8080">$i=</font><font color="#004080">1</font>;<font color="#FF8080">$i</font>&lt;(<font color="#FF8080">$messages</font>+<font color="#004080">1</font>);<font color="#FF8080">$i</font>++){
</p>
<p><font color="#FF8080">$item=$inb</font>-<font color="#804040">&gt;Messages-&gt;item(<font color="#FF8080">$i</font>)</font>;
</p>
<p>date string
  <font color="#FF8080">$timeres=$item</font><font color="#804040">-&gt;TimeReceived();</font>
  <font color="#FF8080">$date_vb</font>=<b><font color="#004080">getdate</font></b>(<font color="#FF8080">$timeres</font>);
</p>
<p>date elements
  <font color="#FF8080">$year=$date_vb</font>['year'];
  <font color="#FF8080">$month=$date_vb</font>['mon'];
  <font color="#FF8080">$day=$date_vb</font>['mday'];
</p>
<p>entering the folder elements
  <font color="#004080"><b>echo</b></font> "<font color="#008080">&lt;tr 
  bgcolor=#F0F0F0&gt;&lt;td&gt;&lt;font face=verdana size=2 color=darkblue&gt;$i&lt;/td&gt;
  &lt;td&gt;&lt;font face=verdana size=2 color=darkblue&gt;
  &lt;a href=view.php?id=$i&amp;folder=$folder target=bottomFrame&gt;
  &lt;font face=verdana size=2 color=#FF6666&gt;$item-&gt;Subject&lt;/font&gt;&lt;/td&gt;&lt;td&gt;
  &lt;font face=verdana size=2 color=darkblue&gt;$item-&gt;Type&lt;/td&gt;&lt;td&gt;
  &lt;font face=verdana size=1 color=darkblue&gt;$year/$month/$day&lt;/td&gt;
  &lt;/font&gt;&lt;tr&gt;</font>";
  }
  <font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;/table&gt;</font>";
  }</p>
<p>&nbsp;</p></code></pre>

<p>view mesage from selected folder (Inbox or Outbox) </p>

<pre><code><p>
  <b><font color="#004080">function</font></b> <font color="#008080">ViewMessageFromFolde</font>r(<font color="#FF8080">$id,$folder</font>){
  create new instance of the COM Objects
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);
  <font color="#FF8080">$session</font>= new <b><font color="#004080">COM</font></b>(<font color="#008080">"MAPI.Session"</font>);
</p>
<p>Log into the current working session
  <font color="#FF8080">$session</font><font color="#804040">-&gt;Logon();</font></p>
<p>get default folder
  <font color="#FF8080">$inb=$session</font><font color="#804040">-&gt;$folder;</font>
</p>
<p><b><font color="#004080">if</font></b>(<font color="#FF8080">$id</font>==""){
  <font color="#004080"><b>echo</b></font> "<font color="#008080">&lt;font 
  face=verdana size=2 color=darkblue&gt;Message Viewer&lt;/font&gt;&lt;br&gt;&lt;font 
  face=verdana size=2 color=red&gt;&lt;center&gt;No Messages Selected&lt;/center&gt;
  &lt;/font&gt;</font>";
  }
  <font color="#004080"><b>else</b></font>{
  <font color="#FF8080">$idint</font>=(int)<font color="#FF8080">$id</font>;</p>
<p>get the messages in the selested folder
  <font color="#FF8080">$items=$inb</font><font color="#804040">-&gt;Messages-&gt;item(<font color="#FF8080">$idint</font>);</font>
</p>
<p>make message status read= true
  <font color="#FF8080">$items</font>-&gt;<font color="#804040">Unread=</font>"<font color="#008080">false</font>";
</p>
<p>Update the message status into Outlook's Inbox
  <font color="#FF8080">$items</font><font color="#804040">-&gt;Update(<b><font color="#004080">true</font></b>);</font>
</p>
<p>display the message </p>
<p><font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;font 
  face=verdana size=2 color=darkblue&gt;Message Viewer&lt;/font&gt;</font>";
  <font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;table 
  width=100%&gt;&lt;tr&gt;&lt;td&gt;&lt;font face=verdana size=2 color=darkblue&gt;$i&lt;/td&gt;
  &lt;td&gt;&lt;font face=verdana size=2 color=darkblue&gt;
  &lt;b&gt;$items-&gt;Subject&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;font face=verdana 
  size=2 color=darkblue&gt;$items-&gt;Type&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/font&gt;&lt;tr&gt;
  &lt;tr&gt;&lt;td colspan=4&gt;&lt;pre&gt;&lt;font face=verdana size=2 color=darkblue&gt;
  $items-&gt;Text&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;</font>";
  }
  }</p>
<p> <b><font color="#004080">function</font></b> <font color="#008080">getUnreadinInbox()</font>{</p>
<p>
  get unread messages from the Inbox Folder
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);
  <font color="#FF8080">$oNs </font>=<font color="#FF8080"> $oOutlook</font><font color="#804040">-&gt;GetNamespace(<font color="#008080">"MAPI"</font>);</font>
  <font color="#FF8080">$oFldr</font> = <font color="#FF8080">$oNs-<font color="#804040">&gt;</font></font><font color="#804040">GetDefaultFolder(<b><font color="#004080">olFolderInbox</font></b>);</font>
  <font color="#FF8080">$UnreadMessagesInFolder</font> = <font color="#FF8080">$oFldr<font color="#804040">-&gt;</font></font><font color="#804040">UnReadItemCount;</font>
  <font color="#004080"><b>return</b> </font><font color="#FF8080">$UnreadMessagesInFolder;</font>
  }</p>
<p><b><font color="#004080">function</font></b> <font color="#008080">getUnreadinOutbox()</font>{
  get unread messages from the Outbox Folder
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);
  <font color="#FF8080">$oNs </font>=<font color="#FF8080"> $oOutlook</font><font color="#804040">-&gt;GetNamespace(<font color="#008080">"MAPI"</font>);</font>
  <font color="#FF8080">$oFldr</font> = <font color="#FF8080">$oNs-<font color="#804040">&gt;</font></font><font color="#804040">GetDefaultFolder(<b><font color="#004080">olFolderOutbox</font></b>);</font>
  <font color="#FF8080">$UnreadMessagesInFolder</font> = <font color="#FF8080">$oFldr<font color="#804040">-&gt;</font></font><font color="#804040">UnReadItemCount;</font>
  <font color="#004080"><b>return</b> </font><font color="#FF8080">$UnreadMessagesInFolder;</font></p>
<p>}</p>
<p><b><font color="#004080">function</font></b> <font color="#008080">staticFolders()</font>{
  // List of the avaailable folders (static !!!)
  <font color="#FF8080">$unread=$this-&gt;</font><font color="#804040">getUnreadinInbox();</font>
  <font color="#FF8080">$out_unr</font>=<font color="#FF8080">$this-&gt;</font><font color="#804040">getUnreadinOutbox();</font>
  <font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;font 
  color=blue face = verdana size=1&gt;Available folders in this version are:
  &lt;a href=comunread.php?folder=Inbox&gt;Inbox(&lt;font color=red&gt;$unread&lt;/font&gt;)
  &lt;/a&gt; and &lt;a href=comunread.php?folder=Outbox&gt;
  Outbox(&lt;font color=red&gt;$out_unr&lt;/font&gt;)&lt;/a&gt;&lt;/font&gt;</font>";
  }</p>
<p>
  //end of classs
  }
  ?&gt;
</p></code></pre>
<p>
after this you are ready for the <b><font color="#FF0000">STEP3:</font></b> 
  Implementation:</p>
<p>Make a new file named: comunread.php</p>
<p>and write:</p>
<pre><code><p>&lt;?PHP
  previous class </p>
<p><b><font color="#004080">require</font></b><font color="#008080">("COutLook.php"</font>);
  make new instance of the class</p>
<p><font color="#FF8080">$class=</font> <b><font color="#004080">new</font></b> 
  <font color="#008080">COutLook</font>;
  <font color="#004080"><b>if </b></font>(<font color="#FF8080">$folder</font>==""){
  <font color="#FF8080">$class-&gt;</font><font color="#804040">staticFolders();</font>
  }
  <font color="#004080"><b>else</b></font> {
  <font color="#FF8080">$class-&gt;</font><font color="#804040">staticFolders();</font>
  <font color="#FF8080">$class-&gt;</font><font color="#804040">getMessages(<font color="#FF8080">$folder</font>);</font>
  }</p>
<p>?&gt;</p>
</code></pre>	  
<p>and new file named view.php</p>
<p>which contains:</p>
<pre><code><p>&lt;?PHP</p>
<p>previous class 
  <b><font color="#004080">require</font></b><font color="#008080">("COutLook.php"</font>);
  <font color="#FF8080">$class=</font> <b><font color="#004080">new</font></b> 
  <font color="#008080">COutLook</font>;</p>
<p>if no messages selected</p>
<p><b><font color="#004080">if</font></b> <font color="#FF8080">($id</font>=="" 
  || <font color="#FF8080">$folder</font>== ""){
  <font color="#004080"><b>echo</b></font> "<font color="#008080">&lt;font 
  face=verdana size=2 color=darkblue&gt;Message Viewer&lt;/font&gt;
  &lt;br&gt;&lt;font face=verdana size=2 color=red&gt;
  &lt;center&gt;No Messages Selected&lt;/center&gt;&lt;/font&gt;</font>";
  }
  <font color="#004080"><b>else</b></font>{</p>
<p>get the message </p>
<p>
  <font color="#FF8080">$class-&gt;</font><font color="#804040">ViewMessageFromFolder(<font color="#FF8080">$id,$folder</font>)</font>;
  }
  ?&gt;</p></code></pre>

<p>and the make a file named <b>index.php</b> which have 2 frames in main frame 
  you should put the <i>'comunread.php</i>' file and in the bottom frame '<i>view.php</i>'</p>
<p>Then run the index.php!</p>
<p>If you have installed <b>Exchange</b> server you must login in your account 
  when you create the session</p>
<p><font color="#FF8080">$session</font><font color="#804040">-&gt;Logon("your 
  name","your password", true or false for the show the logon dialog);</font></p>
<p><font face="Verdana" size="2"><b>Errors:</b></font></p>
<p><font face="Verdana" size="2"><img src="COM_Outlook_files/error.gif" height="30" width="329"></font></p>
<p><font face="Verdana" size="2">If your php returns this error , please check 
  your registry for the COM objects <b>Outlook.application</b> and <b>MAPI.Session,</b></font><font face="Verdana" size="2">or 
  check your syntax</font></p>
<p>&nbsp;</p>
<br>

<hr>
This article was written by Bogomil Shopov and you can visit his personal page 
<a href="http://www.programmersheaven.com/c/authorpage.asp?AuthorID=33303">here</a>.

<br>
<br>

<!--#include virtual="/c/comments/ArticleComments.asp"-->
<!--#include virtual="/include/common/ArticleCounter.inc"-->


<br>
<hr>
Written or thinking of writing an article?  Want to get it on this site, just like this one is?  
If so, click <a href="http://www.programmersheaven.com/other/authorguide.htm">here</a> to find out more! 
<br>


</td>
</tr>
</tbody></table>

<!-- Content end -->

</td>
</tr>
</tbody></table>
<br>

<!--#include virtual="/include/common_footer.inc"-->


</body></html>